% Mengzi Zhang
% CIS 520 hw 6
%

function K = kernel_intersection(X, X2)
% Evaluates the Histogram Intersection Kernel
%
% Usage:
%
%    K = KERNEL_INTERSECTION(X, X2)
%
% For a N x D matrix X and a M x D matrix X2, computes a M x N kernel
% matrix K where K(i,j) = k(X(i,:), X2(j,:)) and k is the histogram
% intersection kernel.

n = size(X,1);
m = size(X2,1);
K = zeros(m, n);

% HINT: Transpose the sparse data matrix X, so that you can operate over columns. Sparse
% column operations in matlab are MUCH faster than row operations.

% YOUR CODE GOES HERE.

% D x n
X_t = X';
% D x m
X2_t = X2';

for i = 1 : m
  
  % D x 1
  a = X2_t (:, i);
  
  % D x n
  intersection = bsxfun (@min, a, X_t);
  
  % 1 x n
  summation = sum (intersection, 1);
  
  % save into kernal matrix K
  K (i, :) = summation;
  
%  
%   a = X2 (i, :);
%   for j = 1 : n
%   
%     b = X_t (:, j);
%     b = b';
%     
%     K (i, j) = sum (min (a, b));
% 
%   end
end

